package com.jp.knowledge.wxapi;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.logic.PersonLogic;
import com.jp.knowledge.my.activity.MyFileActivity;
import com.jp.knowledge.my.activity.UserSettingActivity;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.umeng.socialize.weixin.view.WXCallbackActivity;

/**
 * Created by jiayen on 16/10/10.
 */
public class WXEntryActivity extends WXCallbackActivity implements IWXAPIEventHandler{

    public static final String WECHAT_BIND = "weChatBind";
    public static final String WECHAT_BIND_ERR_CODE = "ErrCode";
    public static final String WECHAT_BIND_RESULT_CODE = "resultCode";


    @Override
    public void onReq(BaseReq req) {
        super.onReq(req);
    }

    @Override
    public void onResp(BaseResp resp) {
        Intent broadcastIntent = new Intent(WECHAT_BIND);
        try {
            SendAuth.Resp response = (SendAuth.Resp) resp;
            /* 请求登陆验证回调 */
            if (response.state != null
                    && response.state.equals(MyFileActivity.WEICHAT_LOGIN_STATE)) {
                broadcastIntent.putExtra(WECHAT_BIND_ERR_CODE, resp.errCode);
                switch (resp.errCode) {
                    case BaseResp.ErrCode.ERR_OK:               // 授权通过
                        broadcastIntent.putExtra(WECHAT_BIND_RESULT_CODE, response.code);
                        break;
                    case BaseResp.ErrCode.ERR_USER_CANCEL:      // 取消授权
                        ToasUtil.toast(this, "微信授权被取消");
                        break;
                    case BaseResp.ErrCode.ERR_AUTH_DENIED:      // 拒绝授权
                        ToasUtil.toast(this, "获取微信授权失败");
                        break;
                    default:
                        ToasUtil.toast(this, "未知错误");
                        break;
                }
                finish();
            } else {
                super.onResp(resp);
            }
        } catch (Exception e){
            finish();
        }
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
    }
}
